package edu.bmstu.rslab;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 * Created by Chikh (using IntelliJ IDEA).
* Date: 15.05.2009
* Time: 13:56:37
*/
class selectPositionMaskAction extends AbstractAction {
    private final VideoPanel videoPanel;
    private final Camera camera;
    private final ArrayList<Point2D> checkPoints;
    private final CoordinateSystemPanel coordinateSystemPanel;
    private final Action nextAction;
    private final Detector detector;
    private final Translator translator;
    private boolean isFirstTime = true;

    public selectPositionMaskAction(Detector detector, Translator translator, VideoPanel videoPanel, Camera camera, ArrayList<Point2D> checkPoints, CoordinateSystemPanel coordinateSystemPanel, Action nextAction) {
        super("Select position mask");
        this.detector = detector;
        this.translator = translator;
        this.videoPanel = videoPanel;
        this.camera = camera;
        this.checkPoints = checkPoints;
        this.coordinateSystemPanel = coordinateSystemPanel;
        this.nextAction = nextAction;
        setEnabled(false);
    }

    public void actionPerformed(ActionEvent e) {
        setEnabled(false);
        final MouseMotionListener mouseMotionHandler = new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                videoPanel.selectingArea(videoPanel.getScaledPoint(e.getPoint()));
            }
        };
        videoPanel.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                videoPanel.beginSelecting(videoPanel.getScaledPoint(e.getPoint()));
                videoPanel.addMouseMotionListener(mouseMotionHandler);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                videoPanel.removeMouseMotionListener(mouseMotionHandler);
                if (isFirstTime) {
                    videoPanel.showMagnifiedImage(videoPanel.getScaledPoint(e.getPoint()));
                    isFirstTime = false;
                }
                else {
                    videoPanel.removeMouseListener(this);
                    detector.setMask(videoPanel.getSelectedMask(videoPanel.getScaledPoint(e.getPoint())));
                    BufferedImage video = camera.getImage();
                    Point2D detectedPosition = detector.detect(video);
                    Point2D translatedPosition = translator.translate(detectedPosition);
                    videoPanel.setDetectedPosition(detectedPosition);
                    checkPoints.add(translatedPosition);
                    coordinateSystemPanel.addPathPoint(translatedPosition);
                    nextAction.setEnabled(true);
                }
            }
        });
    }
}
